import {createRouter, createWebHistory, RouteRecordRaw} from "vue-router";

// 使用页面跳转进度条插件
import NProgress from 'nprogress'; // progress bar
import 'nprogress/nprogress.css';
NProgress.configure({ showSpinner: false });

// 引入静态路由
import constants from "@/router/constants.ts";
// 404路由
import notFound from "@/views/exception/404Page.vue";
// 403路由
import notAuthority from "@/views/exception/403Page.vue";

/**
 * 模块引入与处理
 */
// 引入模块处理函数
import {collectModules} from "@/utils/routerCollecter.ts"
// 导入动态路由
const asyncModulesFiles: any = import.meta.glob('./asyncModules/*.ts', { eager: true });
// 收集模块
const modules = collectModules(asyncModulesFiles);
// 最后的模块处理结果
const asyncModules: RouteRecordRaw[] = [];
// 处理模块
modules.forEach(module => {
    for (let moduleKey in module) {
        asyncModules.push(module[moduleKey]);
    }
});

// 创建路由
const router = createRouter({
    // 启用history模式
    history: createWebHistory(),
    routes: [
        ...constants,
        ...asyncModules,
        {
            path: '/403',
            name: 'notAuthority',
            component: notAuthority
        },
        {
            path: '/:pathMatch(.*)*',
            name: 'NotFound',
            component: notFound
        },
    ]
});

// 引入路由全局守卫
import {beforeHook, afterHook} from "./routerPermission.ts";
// 前置
router.beforeEach(beforeHook);
// 后置
router.afterEach(afterHook);

export default router;

